package com.terrier.ui.controls
{
    import com.terrier.ui.controls.scrollClasses.WxScrollBarBase;
    import com.terrier.ui.skins.WxSkinManager;

    import flash.geom.Point;

    /**
     * 垂直滚动条
     * @author Jarvis.weng
     */
    public class WxVScrollBar extends WxScrollBarBase
    {
        /**
         * 构造函数
         */
        public function WxVScrollBar()
        {
            super();
        }

        /**
         * @private
         */
        override public function setSkinID(skinID:String):void
        {
            setSkin(WxSkinManager.getVScrollBarSkin(skinID));
        }

        /**
         * @private
         */
        override protected function updateLayout():void
        {
			super.updateLayout();
			
			if (!_skin.customLayout)
			{
				track.height = _height - increaseButton.height - decreaseButton.height;
				decreaseButton.y = 0;
				track.y = decreaseButton.height;
				increaseButton.y = track.y + track.height;
				
				track.width = decreaseButton.width = increaseButton.width = thumb.width = _width;
				
				if (pageSize == 0 || range == 0)
				{
					thumb.visible = false;
				}
				else
				{
					thumb.height = track.height * pageSize / (range + pageSize);
					thumb.visible = true;
				}
			}

            if (!isDrag)
            {
                updateThumbPosition();
            }
        }

        /**
         * @private
         */
        override protected function dragThumb(pt:Point):void
        {
            var minPos:Number = track.y;
            var maxPos:Number = track.y + track.height - thumb.height;
            var posRange:Number = maxPos - minPos;
            var yPos:Number = Math.max(minPos, Math.min(pt.y, maxPos));

            thumb.y = yPos;

            value = (yPos - minPos) / posRange * range + minimum;
        }

        /**
         * @private
         */
        override protected function updateThumbPosition():void
        {
            var minPos:Number = track.y;
            var maxPos:Number = track.y + track.height - thumb.height;
            var posRange:Number = maxPos - minPos;

            thumb.y = percentValue * posRange + minPos;
        }

        /**
         * @private
         */
        override protected function pointToValue(pt:Point):Number
        {
            var minPos:Number = 0;
            var maxPos:Number = track.height - thumb.height;
            var posRange:Number = maxPos - minPos;
            var yPos:Number = pt.y;

            return (yPos - minPos) / posRange * range + minimum;
        }
    }
}
